#include "symtab_widget.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QTableWidgetItem>
#include <QTableWidget>
SymtabWidget::SymtabWidget(QWidget *parent)
    : QWidget{parent}
    , tabWidget(new QTableWidget)
{
    initUI();
}

void SymtabWidget::updataData(ELFRead *elfRead,int index)
{
    Elf64_Shdr shdr = elfRead->getSection(index);
    Elf64_Shdr strshdr = elfRead->getSection(shdr.sh_link);
    char * str_p = (char *)elfRead->getBuffer() + strshdr.sh_offset;
    Elf64_Sym *symtable = (Elf64_Sym *)(elfRead->getBuffer() + shdr.sh_offset);
    tabWidget->setRowCount(shdr.sh_size / sizeof(Elf64_Sym));
    tabWidget->setColumnCount(6);
    for(int i = 0 ; i < shdr.sh_size / sizeof(Elf64_Sym);i++){
       Elf64_Sym item = symtable[i];

       setTableItem(i,0,QString(str_p + item.st_name));
       setTableItem(i,1,QString::number(item.st_info));
       setTableItem(i,2,QString::number(item.st_value));
       setTableItem(i,3,QString::number(item.st_other));
       setTableItem(i,4,QString::number(item.st_size));
       setTableItem(i,5,QString::number(item.st_shndx));

    }

}

void SymtabWidget::initUI()
{
    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addWidget(tabWidget);
    setLayout(mainlayout);
}

void SymtabWidget::setTableItem(int row, int col, QString value)
{
    QTableWidgetItem * item = new QTableWidgetItem(value);
    tabWidget->setItem(row,col,item);
}
